package itsm.isperp.control.form.process;

import itsm.isperp.framework.core.context.ContextHolder;
import itsm.isperp.workflow.service.BpmProcessRunService;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

/**
 * 
 * 工作流进度图
 * 
 * @author lizx
 * @date 2013-10-19
 * @since 1.0
 * 
 */
public class WorkflowProcesser {

	public static final String renrder(String processId) {

		BpmProcessRunService bpmProcessRunService = ContextHolder
				.getSpringBean("bpmProcessRunService");

		if (StringUtils.isEmpty(processId)) {
			return "";
		}

		Map<String, Object> map = bpmProcessRunService.trace(processId);

		List<Map<String, Object>> historys = (List<Map<String, Object>>) map
				.get("historys");

		Processer p = new Processer();
		int i = 0;
		for (Map<String, Object> h : historys) {
			String name_ = (String) h.get("name_");

			if (StringUtils.isNotEmpty(name_)) {
				p.addProcessNode(name_, (Date) h.get("action_time"),
						(String) h.get("action_name"),
						"<a class='proce-node-ready' onclick=\"isperp.viewUser(\'"
								+ (String) h.get("action_user_id") + "\')\">"
								+ (String) h.get("fullname") + "</a>",
						(String) h.get("comment"));
			} else {

				p.addProcessNode(i == 0 ? "开始" : "",
						(Date) h.get("action_time"),
						(String) h.get("action_name"),
						"<a class='proce-node-ready' onclick=\"isperp.viewUser(\'"
								+ (String) h.get("action_user_id") + "\')\">"
								+ (String) h.get("fullname") + "</a>",
						(String) h.get("comment"));
			}
			i++;
		}

		String areRunning = (String) map.get("areRunning");

		if (StringUtils.isNotEmpty(areRunning)) {
			String currentTaskName = (String) map.get("currentTaskName");

			if (StringUtils.isNotEmpty(currentTaskName)) {

				List<Map<String, Object>> listUser = (List<Map<String, Object>>) map
						.get("currentUserList");

				String cUser = "";
				for (Map<String, Object> m : listUser) {
					cUser += ",<a class='proce-node-current' onclick=\"isperp.viewUser(\'"
							+ (String) m.get("name")
							+ "\')\">"
							+ m.get("fullname") + "</a>";
				}

				p.addProcessNode(currentTaskName, null, null,
						cUser.substring(1), null);
			}
		} else {
			p.setEnd(true);
		}

		return p.renderHtml();
	}
	public static final String renrder(String processId,boolean ifShowProceReady) {

		BpmProcessRunService bpmProcessRunService = ContextHolder
				.getSpringBean("bpmProcessRunService");

		if (StringUtils.isEmpty(processId)) {
			return "";
		}

		Map<String, Object> map = bpmProcessRunService.trace(processId);

		List<Map<String, Object>> historys = (List<Map<String, Object>>) map
				.get("historys");

		Processer p = new Processer();
		int i = 0;
		for (Map<String, Object> h : historys) {
			String name_ = (String) h.get("name_");

			if (StringUtils.isNotEmpty(name_)) {
				p.addProcessNode(name_, (Date) h.get("action_time"),
						(String) h.get("action_name"),
						"<a class='proce-node-ready' onclick=\"isperp.viewUser(\'"
								+ (String) h.get("action_user_id") + "\')\">"
								+ (String) h.get("fullname") + "</a>",
						(String) h.get("comment"));
			} else {

				p.addProcessNode(i == 0 ? "开始" : "",
						(Date) h.get("action_time"),
						(String) h.get("action_name"),
						"<a class='proce-node-ready' onclick=\"isperp.viewUser(\'"
								+ (String) h.get("action_user_id") + "\')\">"
								+ (String) h.get("fullname") + "</a>",
						(String) h.get("comment"));
			}
			i++;
		}

		String areRunning = (String) map.get("areRunning");

		if (StringUtils.isNotEmpty(areRunning)) {
			String currentTaskName = (String) map.get("currentTaskName");

			if (StringUtils.isNotEmpty(currentTaskName)) {

				List<Map<String, Object>> listUser = (List<Map<String, Object>>) map
						.get("currentUserList");

				String cUser = "";
				for (Map<String, Object> m : listUser) {
					cUser += ",<a class='proce-node-current' onclick=\"isperp.viewUser(\'"
							+ (String) m.get("name")
							+ "\')\">"
							+ m.get("fullname") + "</a>";
				}

				p.addProcessNode(currentTaskName, null, null,
						cUser.substring(1), null);
			}
		} else {
			p.setEnd(true);
		}

		return p.renderHtml(ifShowProceReady);
	}
}
